<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Base\ServerList;
use App\Models\Base\StatResource;
use App\Models\Base\StatResourcesCollect;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class ResourceController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.data_statistic'));
            $content->description(trans('game.user_Resource_records'));
            $content->row($this->box());
            $content->body($this->grid(1));
        });
    }

    protected function box()
    {
        $inputs['date_time'] = request()->input('date_time');
        $inputs['server_id'] = request()->input('server_id');
        $inputs['resource_type'] = request()->input('resource_type');
        $where = [];
        if ($inputs) {
            foreach ($inputs as $key => $value) {
                if ($value) {
                    $where[] = [$key, '=', $value];
                }
            }
        }
        $data = StatResourcesCollect::where($where)->selectRaw('sum(totality) as totalitys,sum(inventory) as inventorys,sum(quantity) as quantitys, avg(proportion) as proportions')->first()->toArray();
        if (!$data) {
            return '';
        }

        $str = '<p>'.trans('game.tot_number').':'.$data['totalitys'].'&nbsp;&nbsp;'.trans('game.tot_inventory'). ':'.$data['inventorys'].'&nbsp;&nbsp;
        '.trans('game.tot_consume').':'.$data['quantitys'].trans('game.proportion').':'.floatPercent($data['proportions']).'</p>';

        return $str;
    }

    //资源获取记录
    public function getRecord()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.data_statistic'));
            $content->description(trans('game.user_Resource_records'));
            $content->body($this->grid(2));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($way_type)
    {
        return Admin::grid(StatResource::class, function (Grid $grid) use ($way_type) {
            $grid->model()->where('way_type', $way_type)->orderBy('id', 'desc');
            $grid->id('ID')->sortable();
            $grid->date_time(trans('game.date'));
            $grid->resource_type(trans('game.type'))->display(function ($type) {
                switch ($type) {
                    case '1':
                        return trans('game.coin');
                        break;
                    case '2':
                        return trans('game.gold');
                        break;
                    case '3':
                        return trans('game.crystal');
                        break;
                    case '4':
                        return trans('game.stone');
                        break;
                }
            });
            $grid->number(trans('game.count'));
            $grid->getEvent()->name(trans('game.event_cause'));

            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->date('date_time', trans('game.date'));
                $filter->equal('server_id', trans('game.server_name'))->select(ServerList::all()->pluck('name', 'server_id'));
                $filter->equal('resource_type', trans('game.type'))->select([1 => trans('game.coin'), 2 => trans('game.gold'), 3 => trans('game.crystal'), 4 => trans('game.stone')]);
            });
            $grid->disableExport();
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableRowSelector();
        });
    }
}
